var form;
var layer;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    $.removeTableCookie('SpryMedia_DataTables_dictTable_dictList.jsp');
    getDataTable();

    //新增
    $("#addDict").click(function () {
        saveOrUpdateDict(null);
    });
    // 查询
    $("#search").click(function () {
        $.removeTableCookie('SpryMedia_DataTables_dictTable_dictList.jsp');
        getDataTable(); //要触发的方法
        return false;
    });

    //回车事件
    $(document).keydown(function (event) {
        var code = event.which;
        if (code == 13) {
            $.removeTableCookie('SpryMedia_DataTables_dictTable_dictList.jsp');
            getDataTable(); //要触发的方法
            return false;
        }
    });
});
/**
 * 获取列表
 */
function getDataTable() {
    $('#dictTable').dataTable({
        'bProcessing': true,
        'bServerSide': true,
        'fnServerParams': function (aoData) {
            var name = $.trim($("#name").val());
            var type = $.trim($("#type").val());
            aoData.push({
                "name": "dict.name",
                "value": name
            }, {
                "name": "dict.type",
                "value": type
            });
        },
        "sAjaxSource": basePath + "dict/getDictList.action", // 获取列表
        "sServerMethod": "POST",
        "sPaginationType": "full_numbers",
        "bPaginate": true, // 翻页功能
        "bStateSave": true, // 状态保存
        "bLengthChange": false, // 改变每页显示数据数量
        "bFilter": false, // 过滤功能
        "bSort": false, // 排序功能
        "bInfo": true, // 页脚信息
        "bAutoWidth": false, // 自动宽度
        "bDestroy": true,
        "iDisplayLength": 15, // 每页显示多少行
        "aoColumns": [{
            "sTitle": '排序',
            "mDataProp": "no"
        }, {
            "sTitle": '名称',
            "mDataProp": "name",
            "sClass": "conceal"
        }, {
            "sTitle": '值',
            "mDataProp": "value"
        }, {
            "sTitle": '类型',
            "mDataProp": "type"
        }, {
            "sTitle": '备注',
            "mDataProp": "remark",
            "sClass": "conceal"
        }, {
            "sTitle": '创建人',
            "mDataProp": "createUserName"
        }, {
            "sTitle": '创建时间',
            "mDataProp": "createTime"
        }, {
            "sTitle": '操作',
            "mDataProp": null
        }],
        "oLanguage": {
            "sUrl": basePath + "plugins/datatable/cn.txt" // 中文包
        },
        "fnDrawCallback": function (oSettings) {
            $('#dictTable tbody  tr td').each(function () {
                this.setAttribute('title', $(this).text());
            });
        },
        "fnInitComplete": function () {
        },
        "aoColumnDefs": [{
            "aTargets": [7],
            "fnRender": function (oObj) {
                var dictId = oObj.aData.id;
                return '<a data-method="repair" class="layui-btn layui-btn-small layui-btn-normal layui-btn-radius" onclick=saveOrUpdateDict(\'' + dictId + '\')>修改</a>'
                    + '<a data-method="del" class="layui-btn layui-btn-small layui-btn-danger layui-btn-radius" onclick=deleteDict(\'' + dictId + '\')>删除</a>';
            }
        }]
    });
}

/**
 * 新增/修改
 */
function saveOrUpdateDict(dictId) {
    var url;
    var titleVal;
    if (null == dictId) {
        url = "jsp/dict/addOrUpdateDict.jsp";
        titleVal = "新增数据字典";
    } else {
        url = "dict/toUpdate.action?type=update&id=" + dictId;
        titleVal = "修改数据字典";
    }
    layer.open({
        type: 2,
        title: titleVal,
        area: ["50%", "45%"],
        shade: 0.3,
        maxmin: true,
        content: basePath + url,
        zIndex: layer.zIndex,
        success: function (layero) {//弹出后回调
            layer.setTop(layero); //弹窗置于最上层
        }
    });
}

/**
 * 删除
 */
function deleteDict(dictId) {
    layer.confirm('确定删除此条数据?', {
        title: " 确认",
        icon: 3,
        skin: 'layui-layer-molv',
        btn: ["确定", "取消"],
        yes: function (index) {
            $.ajax({
                url: basePath + "dict/deleteDict.action?id=" + dictId,
                type: "post",
                dataType: 'text',
                success: function (data) {
                    if (data != null && data != "" && data == 0) {
                        getDataTable();
                    } else {
                        layer.open({
                            title: '错误',
                            icon: 2,
                            content: '删除失败！'
                        });
                        getDataTable();
                    }
                }
            });
            layer.close(index);
        }
    });
}